﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using Newtonsoft.Json;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.ServiceComponent.DAC.Service;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Base.Dao;

namespace T.FAS.Runtime.DataModel.Dao
{
    public class StateMachineActionELBDao : DaoService<TAction>
    {
        public override string TableName => "FAS_StateMachineAction";
        public override string IDColumnName => "ID";
        public string ForeignColumn => "TransitionID";
        public string MetaDataForeignColumn => "StateMachineID";

        public override List<string> ExceptColumnName {get;} = DaoUtil.GetExceptionColumn<TAction>();

        public StateMachineActionELBDao() : base()
        {
            DBName = "TFAS";
        }

        /// <summary>
        /// 获取元数据内容
        /// </summary>
        /// <param name="metaDataId"></param>
        /// <returns></returns>
        public List<TAction> GetTActions(string tranId)
        {
            var result = new List<TAction>();
            if (string.IsNullOrEmpty(tranId))
            {
                return result;
            }
            var dt = base.GetByFilter($"{ForeignColumn}='{tranId}'");
            result = DaoUtil.ConvertToEntity<TAction>(dt, this.ExceptColumnName) ?? result;
            return result.OrderBy(o => o.Sort).ToList();
        }

        /// <summary>
        /// 获取元数据内容
        /// </summary>
        /// <param name="metaDataId"></param>
        /// <returns></returns>
        public List<TAction> GetByMetaDataForeignKey(string smId)
        {
            var result = new List<TAction>();
            if (string.IsNullOrEmpty(smId))
            {
                return result;
            }
            var dt = base.GetByFilter($"{MetaDataForeignColumn}='{smId}'");
            result = DaoUtil.ConvertToEntity<TAction>(dt, this.ExceptColumnName) ?? result;
            return result.OrderBy(o => o.Sort).ToList();
        }

        /// <summary>
        /// 添加TAction
        /// </summary>
        /// <param name="actions"></param>
        public void CreateTAction(List<TAction> actions)
        {
            try
            {
                DaoUtil.ExecuteInsertSQL(actions, this);    //创建Action
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.SM.CreateActionError, e.Message, e);
            }
        }

        /// <summary>
        /// 删除TAction
        /// </summary>
        /// <param name="tranId"></param>
        public void DeleteTActionByForeignKey(string tranId)
        {
            try
            {
                base.DB.ExecSqlStatement(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, this.ForeignColumn, tranId));
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.SM.DeleteActionError, e.Message, e);
            }
        }

        /// <summary>
        /// 删除TAction
        /// </summary>
        /// <param name="smId"></param>
        public void DeleteByMetaDataForeignKey(string smId)
        {
            try
            {
                base.DB.ExecSqlStatement(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, MetaDataForeignColumn, smId));
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.SM.DeleteActionError, e.Message, e);
            }
        }
    }
}
